package com.atbeijing.D02;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * flatMap用法
 * flatMap是map和filter的泛化
 * flatMap是比map和filter更加底层的算子，可以用flatMap来实现map和filter的功能
 */
public class Example4 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        DataStreamSource<String> source = env.fromElements("white","white",  "black", "black","gray");

        source.flatMap(new FlatMapFunction<String, String>() {
            @Override
            public void flatMap(String value, Collector<String> collector) throws Exception {
                if (value.equals("white")){
                    collector.collect("1");
                }
                if (value.equals("black")){
                    collector.collect(value);
                    collector.collect(value);
                }
            }
        }).print();

        env.execute();
    }
}
